package leetcode100;

import java.util.HashMap;

public class lc054 {

    public ListNode detectCycle(ListNode head) {
        if (head==null)  {
            return null;
        }

        HashMap<ListNode, Integer> map = new HashMap<>();
        ListNode cur = head;
        while(cur!=null) {
            map.put(cur,map.getOrDefault(cur,0)+1);
            if (map.get(cur)==2) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
}
